package com.hjx.windowtool.bk;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.hjx.windowtool.db.Tool;
import com.hjx.windowtool.xiaoshuo.XiaoshuoTest;
import com.hjx.windowtool.xiaoshuo.XiaoshuoTest.System;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

@Tool("下载小说")
public class BookDownloadApp {

	private JFrame frame;
	private JTextField txt_url;
	private JTextField txt_start;
	private JTextField txt_end;
	private JTextField txt_errorSize;
	private JTextField txt_emptySize;
	private JCheckBox checkBox;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					BookDownloadApp window = new BookDownloadApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public BookDownloadApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 687, 486);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.3);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel("地址");
		label.setBounds(25, 10, 54, 15);
		panel.add(label);
		
		txt_url = new JTextField();
		txt_url.setBounds(89, 7, 340, 21);
		panel.add(txt_url);
		txt_url.setColumns(10);
		
		JButton button = new JButton("下载");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button.setEnabled(false);
				new Thread(new Runnable() {
					public void run() {
						String url = txt_url.getText();
						Integer start = Integer.valueOf(txt_start.getText());
						Integer end = Integer.valueOf(txt_end.getText());
						Integer errorSize = Integer.valueOf(txt_errorSize.getText());
						Integer emptySize = Integer.valueOf(txt_emptySize.getText());
						boolean isAdd = checkBox.isSelected();
						com.hjx.windowtool.xiaoshuo.XiaoshuoTest.System.out = new com.hjx.windowtool.xiaoshuo.XiaoshuoTest.System() {
							public void println(String s) {
								apend(s);
							}

						};
						try {
							XiaoshuoTest.loadLzzxs(url, start, end, isAdd, errorSize, emptySize);
						} catch (Exception e1) {
							try {
								e1.printStackTrace(new PrintStream("") {
									public void println(Object x) {
										apend(String.valueOf(x));
									};
								});
							} catch (FileNotFoundException e2) {
								e2.printStackTrace();
							}
						}finally {
							button.setEnabled(true);
						}
					}
				}).start();
			}
		});
		button.setBounds(440, 6, 93, 23);
		panel.add(button);
		
		JLabel label_1 = new JLabel("起始");
		label_1.setBounds(25, 35, 54, 15);
		panel.add(label_1);
		
		txt_start = new JTextField();
		txt_start.setText("1");
		txt_start.setBounds(89, 32, 35, 21);
		panel.add(txt_start);
		txt_start.setColumns(10);
		
		JLabel label_2 = new JLabel("-");
		label_2.setBounds(127, 35, 13, 15);
		panel.add(label_2);
		
		txt_end = new JTextField();
		txt_end.setText("9999");
		txt_end.setBounds(144, 32, 35, 21);
		panel.add(txt_end);
		txt_end.setColumns(10);
		
		JLabel label_3 = new JLabel("错误");
		label_3.setBounds(189, 35, 35, 15);
		panel.add(label_3);
		
		txt_errorSize = new JTextField();
		txt_errorSize.setText("50");
		txt_errorSize.setBounds(218, 32, 27, 21);
		panel.add(txt_errorSize);
		txt_errorSize.setColumns(10);
		
		JLabel label_4 = new JLabel("空白");
		label_4.setBounds(255, 35, 35, 15);
		panel.add(label_4);
		
		txt_emptySize = new JTextField();
		txt_emptySize.setText("10");
		txt_emptySize.setBounds(289, 32, 35, 21);
		panel.add(txt_emptySize);
		txt_emptySize.setColumns(10);
		
		checkBox = new JCheckBox("叠加");
		checkBox.setSelected(true);
		checkBox.setBounds(326, 31, 103, 23);
		panel.add(checkBox);
		
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setRightComponent(scrollPane);
		
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		scrollPane.setViewportView(textArea);
	}
	private void apend(String s) {
		textArea.append(s+"\n\r");
	}
}
